<?php
class Upload{
//	类型
	private $allowType;
//	路径
	private $path;
//	错误信息
	private $error;
//	尺寸大小
	private $size;
	public $dest;
	
//	初始默认不传参 信息设置	
	public function __construct($path=NULL,$allowType=NULL,$size=NULL){
		$this->path = is_null($path)?C('path'):$path;
		$this->allowType = is_null($allowType)?C('allowType'):$allowType;
		$this->size = is_null($size)?C('size'):$size;		
	}
	
	
//	上传
	public function up(){
//		重组数组
		$arr = $this->reset_arr();
		foreach ($arr as $v){
			if(!$this->filter($v)){
				echo '上传错误！错误类型： ' .$v['name']. $this->error;
				return false;
			}
		}
		foreach ($arr as $v){
			if($this->filter($v)){
				$this->move($v);
			}
		}					
		
	}
	
//重组数组
private function reset_arr(){
//		p($_FILES);
	$arr = array();
	foreach ($_FILES as $v) {
		if(is_array($v['name'])){
			foreach ($v['name'] as $key => $value) {
				$arr[] = array(
					'name' 		=> $value,
					'tmp_name'	=> $v['tmp_name'][$key],
					'error'		=> $v['error'][$key],
					'type'		=> $v['type'][$key],
					'size'		=> $v['size'][$key],
				);
			}
		}else{
			$arr[] = $v;
		}
		
	}
	return $arr;
}
	
//筛选
private function filter($v){
	$type = ltrim(strrchr($v['name'], '.'),'.');
	switch (true) {
				//4.4种错误
		case $v['error'] == 1 :
			$this->error = '大小超过了 php.ini 中 upload_max_filesize 限制值';
			return false;
		case $v['error'] == 2 ;
			$this->error = '文件超过表单允许上传尺寸'. round($this->size/1000/1000,2) .'kb' ;
			return false;
		case $v['error'] == 3;
			$this->error = '文件上传失败,上传文件不完整';
			return false;
//			忽略这个错误  
/*		case $v['error'] == 4;
			echo '上传空';
			$this->error ='上传文件空';*/
			return false;
		//1.是否是合法上传文件
		case !is_uploaded_file($v['tmp_name']):
			$this->error = '不是一个合法上传文件';
			return false;
		//2.类型不允许
		case !in_array($type, $this->allowType):
			$this->error = '上传类型不允许';
			return false;
		//3.大小不允许
		case $v['size'] > $this->size:
			$this->error = '上传文件超过网站配置大小';
			return false;

		default:
			return true;
	}
	
}

/**
 * 移动上传
 */
public function move($v){
	$path = rtrim(str_replace('\\', '/', $this->path),'/');
	is_dir($path) || mkdir($path,0777,true);
	$type = ltrim(strrchr($v['name'], '.'),'.');
	//组合路径
	$dest = $path . '/' . time() . mt_rand(0, 9999) . '.' . $type;
	$this->dest =$dest;
	//移动上传
	move_uploaded_file($v['tmp_name'], $dest);
//	echo $v['name'].'上传成功<br />';
	}
	
	
	
	
	
}




 ?>

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
